//
//  YPAccountDetailViewController.m
//  ChengHuiTong
//
//  Created by xzm on 16/11/24.
//  Copyright © 2016年 张文权. All rights reserved.
//

#import "YPAccountDetailViewController.h"

@interface YPAccountDetailViewController ()<UIScrollViewDelegate>{
    
    UIButton *_lastBtn;
    UIView *_scrollLine;
}

@property (nonatomic,weak)UIScrollView *bgScrollView;

@end

@implementation YPAccountDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"账户详情";
    
    [self setupUI];
}

- (void)setupUI{
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    CGFloat topW = SCREEN_WIDTH/2.f;
    CGFloat topH = 40;
    CGFloat scrollLineH = 2;
    
    YPBaseScrollView *bgScrollView = [[YPBaseScrollView alloc]initWithFrame:CGRectMake(0, topH + scrollLineH, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - topH - scrollLineH)];
    bgScrollView.pagingEnabled = YES;
    bgScrollView.delegate = self;
    bgScrollView.showsHorizontalScrollIndicator = NO;
    bgScrollView.contentSize = CGSizeMake(SCREEN_WIDTH*2, bgScrollView.height);
    [self.view addSubview:bgScrollView];
    self.bgScrollView = bgScrollView;
    
    
    for (int i = 0; i < 2; i ++) {
        
        UIButton *btn = [UIButton creatWithFrame:CGRectMake(topW*i, 0, topW, topH)
                                            font:[UIFont systemFontOfSize:16]
                                            text:i==0?@"账户总资产":@"累计收益"
                                          action:@selector(topBtnClick:)
                                          target:self];
        btn.tag = i+100;
        [btn setTitleColor:kColor_AppNavColor forState:UIControlStateSelected];
        [btn setTitleColor:kColor_greyAshTitleColor forState:0];
        if (i == 0) {
            _lastBtn = btn;
            _lastBtn.selected = YES;
            [self.view addLineWithFrame:CGRectMake(btn.right, btn.top + 10, 0.5, btn.height - 20) color:colorWithRGB(220, 220, 220)];
            
            _scrollLine = [[UIView alloc]initWithFrame:CGRectMake(0, btn.bottom, btn.width, 2)];
            _scrollLine.backgroundColor = kColor_AppNavColor;
            [self.view addSubview:_scrollLine];
            
            [self.view addLineWithFrame:CGRectMake(0, _scrollLine.bottom, SCREEN_WIDTH, 0.5) color:colorWithRGB(240, 240, 240)];
            
        }
        [self.view addSubview:btn];
        
        
        UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, bgScrollView.height)];
        NSString *urlString;
        if (i == 0) {
            urlString = [NSString stringWithFormat:@"%@index.php?m=app&c=account&a=accountAsset&token=%@",kAppHostAddress,[YPShareAccountInfo shareAccountInfo].token];
        }else{
            urlString = [NSString stringWithFormat:@"%@index.php?m=app&c=account&a=accountIncome&token=%@",kAppHostAddress,[YPShareAccountInfo shareAccountInfo].token];
        }
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
        [bgScrollView addSubview:webView];
        
    }
    
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

#pragma mark -- 事件响应
- (void)topBtnClick:(UIButton *)btn{
    
    _lastBtn.selected = NO;
    btn.selected = YES;
    _lastBtn = btn;
    _scrollLine.left = btn.left;
    
    _bgScrollView.contentOffset = CGPointMake((btn.tag-100)*_bgScrollView.width, 0);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    _scrollLine.left = scrollView.contentOffset.x/2.f;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    _scrollLine.left = scrollView.contentOffset.x/2.f;
    NSInteger tag = scrollView.contentOffset.x > scrollView.width/2?101:100;
    UIButton *btn = [self.view viewWithTag:tag];
    [self topBtnClick:btn];
}


@end
